package com.yanzhen.controller;


import com.github.pagehelper.PageInfo;
import com.yanzhen.jwt.JwtUtil;
import com.yanzhen.model.ApplyEvent;
import com.yanzhen.model.Team;
import com.yanzhen.service.ApplyEventService;
import com.yanzhen.service.ITeamService;
import com.yanzhen.service.impl.TeamServiceImpl;
import com.yanzhen.util.JsonObject;
import com.yanzhen.util.R;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王文龙
 * @since 2023-05-13
 */
@Api(tags = {"用户申请参加活动管理"})
@RestController
@RequestMapping("/applyEvent")
public class ApplyEventController {
    @Resource
    private ActivityController activityController;

    @Resource
    private ITeamService teamService;
    @Resource
    private ApplyEventService applyEventService;

    /**
     * 高级分页查询接口
     */
    @RequestMapping("/queryAppEventList")
    public JsonObject queryAppEventList(ApplyEvent applyEvent,
                                        @RequestParam(defaultValue="1") int page,
                                        @RequestParam(defaultValue="10") int limit,
                                        HttpServletRequest httpServletRequest){
        //通过header获取token
        String token = httpServletRequest.getHeader("token");
        //通过token获取角色类型和对象
        Integer type = JwtUtil.getUserType(token);
        if (type==0){//如果是一个社团管理员，只能查询该社团下的活动
            Integer userId = JwtUtil.getUserId(token);
            //根据用户id查询所属社团id
            Team team=teamService.queryTeamInfoByUserId(userId);
            Integer teamId = team.getId();

        }

        //创建返回结果集对象
        JsonObject jsonObject=new JsonObject();
        PageInfo<ApplyEvent> pageInfo = applyEventService.queryAppEventByPage(page, limit, applyEvent);
        jsonObject.setCode(20000);
        jsonObject.setData(pageInfo.getList());
        jsonObject.setTotal(pageInfo.getTotal());
        return jsonObject;
    }


    /**
     * 审核记录接口
     */
    @PutMapping("/checkInfo")
    public R addCheckInfo(@RequestBody ApplyEvent applyEvent,
                          HttpServletRequest httpServletRequest){
        /**
         * 1.接收前端传值的数据
         *  id: 审核表的id
         *  checkContent:审核内容
         *   审核时间，审核人(登录者 token解析出来)，审核状态
         *
         */
        //通过header获取token
        String token = httpServletRequest.getHeader("token");
        //通过token获取角色类型和对象
        Integer userId = JwtUtil.getUserId(token);
        applyEvent.setCheckId(userId);//获取审核者id
        applyEvent.setCheckCreatetime(LocalDateTime.now());
        applyEventService.update(applyEvent);
        return R.ok();
    }
    /**
     * 删除申请记录接口
     */
    @DeleteMapping("/deleteById")
    public R delete(int id){
        applyEventService.deleteById(id);
        return R.ok();
    }
}

